package com.dkm.biz.gl.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;

import com.dkm.biz.gl.domain.GlSwift;
import com.dkm.biz.gl.persistence.dao.GlSwiftQueryDao;
import com.dkm.biz.gl.persistence.entity.GlSwiftPo;
import com.dkm.biz.gl.repository.GlSwiftRepository;
import com.dkm.biz.gl.service.GlSwiftService;

/**
 * 对象功能:流水号 生成
 * 开发公司:SKG.COM
 * 开发人员:RuanYangFa
 * 创建时间:2015-01-20 10:32:40
 */
@Service
public class GlSwiftServiceImpl implements GlSwiftService
{
	@Resource
	private GlSwiftQueryDao glSwiftQueryDao;
	@Resource
	private GlSwiftRepository glSwiftRepository;
	
	public Long getNo(String key){
		return Long.parseLong(nextId(key));
	}
	
	public synchronized String nextId(String key){
		String nextId=GlSwiftUtils.initNum;
		
		GlSwiftPo glSwiftPo	= glSwiftQueryDao.getBySwiftKey(key);
		GlSwift glSwift		= glSwiftRepository.newInstance(glSwiftPo);
		
		if(glSwiftPo!=null&&glSwift!=null){
			
			String regulation,genPeriod,newDate,dbDate;Integer curValue,iniValue,stepNum,len;
			
			regulation	= glSwiftPo.getRegulation();
			stepNum		= glSwiftPo.getStep();
			genPeriod	= glSwiftPo.getGenPeriod();
			curValue	= glSwiftPo.getCurValue();
			dbDate 		= glSwiftPo.getCurDate();
			iniValue    = glSwiftPo.getInitValue();
			newDate 	= GlSwiftUtils.getCurDate();
			len         = glSwiftPo.getLen();
			curValue	= (curValue==null)?iniValue:curValue;
			
			
			if (GlSwiftUtils.diffDate(genPeriod, dbDate))
			{
				glSwiftPo.setCurDate(newDate);
				curValue = iniValue;
			} else
			{
				curValue = curValue + stepNum;
			}
			
			glSwiftPo.setCurValue(curValue);
			glSwift.setData(glSwiftPo);
			glSwift.update();
			
			nextId=GlSwiftUtils.getRegulation(regulation,len,curValue);
		}
		
		return nextId;
	}
}

class GlSwiftUtils{
	
	public  final static String initNum="0";
	
	public static boolean diffDate(String dbGenPeriod,String dbCurDate){
		Integer curDate = Integer.parseInt(parseDate(new Date()));
		Integer genDate = getGenPeriod(dbGenPeriod);
		Integer addDays = addDate(dbCurDate,genDate);
		return (curDate>=addDays);
	}
	
	public static Integer getGenPeriod(String date){
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("day",1);map.put("week",7);map.put("month",30);map.put("quarter",90);map.put("year",365);
		if(map.get(date)!=null){
			return map.get(date);
		}else{
			return 0;
		}
	}

	public static String getCurDate(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(new Date());
	}
	
	public static Integer addDate(String dbCurDate,Integer genDate){
		return Integer.parseInt(parseDate(DateUtils.addDays(formatDate(dbCurDate),genDate)));
	}
	
	public static String parseDate(Date d){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(d);
	}
	
	public static String parseDate(String d){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(d);
	}
	
	public static Date formatDate(String d){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		Date date;
		try{
			date = formatter.parse(d);
		} catch (ParseException e){
			date=new Date();
		}
		return date;
	}
	
	public static String formatTime(int time){
		return (time<10)?initNum + time :String.valueOf(time);
	}

	public static int getYear(java.util.Date date){
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.YEAR);
	}

	public static int getMonth(java.util.Date date){
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.MONTH) + 1;
	}

	public static int getDay(java.util.Date date){
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.DAY_OF_MONTH);
	}
	
	public static String formatNo(Integer curValue,Integer length){
		String no=curValue.toString();
		
		if(no.length()>length) 
			return no;
		
		StringBuffer sb=new StringBuffer();
		for(int i=1;i<=(length-no.length());i++){
			sb.append(initNum);
		}
		sb.append(no);
		return sb.toString();
	}
	
	public static String replaceDate(String regulation,int year_,int month_,int day_,int Len,  int genNo_){
		String month,day,genNo;
		
		month = GlSwiftUtils.formatTime(month_);
		day   = GlSwiftUtils.formatTime(day_);
		genNo = GlSwiftUtils.formatNo(genNo_,Len);
		
		regulation=regulation.replace("{yyyy}",String.format("%04d", year_));
		regulation=regulation.replace("{MM}",month).replace("{mm}",String.format("%02d", month_));
		regulation=regulation.replace("{DD}",day).replace("{dd}",String.format("%02d", day_));
		regulation=regulation.replace("{NO}",genNo).replace("{no}",String.valueOf(genNo));
		
		return regulation;
	}
	
	public static String getRegulation(String regulation,int Len,  int curValue){
		int year,month,day;
		year	 	= GlSwiftUtils.getYear(new Date());
		month	 	= GlSwiftUtils.getMonth(new Date());
		day		 	= GlSwiftUtils.getDay(new Date());
		regulation	= StringUtils.deleteWhitespace(regulation);
		
		return replaceDate(regulation,year,month,day,Len,curValue);
	}
}
